//package hz.mobileproject.pda.firesafety.activity;
//
//import android.content.DialogInterface;
//import android.content.Intent;
//import android.media.AudioManager;
//import android.media.SoundPool;
//import android.os.Bundle;
//import android.support.annotation.Nullable;
//import android.support.v7.app.AlertDialog;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.AdapterView;
//import android.widget.Button;
//import android.widget.EditText;
//import android.widget.GridView;
//import android.widget.ImageButton;
//import android.widget.RelativeLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.blankj.utilcode.util.LogUtils;
//import com.blankj.utilcode.util.SPUtils;
//import com.blankj.utilcode.util.StringUtils;
//import com.blankj.utilcode.util.TimeUtils;
//import com.blankj.utilcode.util.ToastUtils;
//import com.google.gson.Gson;
//import com.google.gson.reflect.TypeToken;
//import com.google.zxing.activity.CaptureActivity;
//
//import org.json.JSONException;
//import org.json.JSONObject;
//
//import java.io.File;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.List;
//
//import cn.droidlover.xdroidmvp.mvp.XActivity;
//import hz.mobileproject.pda.R;
//import hz.mobileproject.pda.broadcast.HandleBroadcast;
//import hz.mobileproject.pda.broadcast.IShowBarcodeStr;
//import hz.mobileproject.pda.entity.Photo;
//import hz.mobileproject.pda.entity.request.Request_fire_end;
//import hz.mobileproject.pda.entity.request.Request_loadPoliceInfo;
//import hz.mobileproject.pda.firesafety.bean.Request_queryFireFighting;
//import hz.mobileproject.pda.firesafety.bean.Request_start_fire;
//import hz.mobileproject.pda.entity.response.Response_loadPoliceInfo;
//import hz.mobileproject.pda.firesafety.bean.Response_queryfire;
//import hz.mobileproject.pda.network.Constance;
//import hz.mobileproject.pda.firesafety.presenter.PStartFireQingDian;
//import hz.mobileproject.pda.ui.adapter.DeptCheckGVAdapter;
//import hz.mobileproject.pda.ui.adapter.FireListRecyclerViewAdapter;
//import hz.mobileproject.pda.ui.view.CustomPopWindow;
//import hz.mobileproject.pda.ui.view.LoadingDialog;
//import hz.mobileproject.pda.utils.CommonUtil;
//import hz.mobileproject.pda.utils.TakePhotoUtil;
//
//import static hz.mobileproject.pda.utils.TakePhotoUtil.OPEN_PIC_RESULT_CODE;
//import static hz.mobileproject.pda.utils.TakePhotoUtil.TAKE_PHOTO_RESULT_CODE;
//
///**
// * 消防器材条目 和扫描界面
// */
//public class StartFireSearchActivity extends XActivity<PStartFireQingDian>implements IShowBarcodeStr {
//
//    private static final String TAG = "StartFireSearchActivity";
//
//    private LoadingDialog loadingDialog;
//    private HandleBroadcast handleBroadcast;
//    private SoundPool soundpool;
//    private int soundid;
//
//    private String police_taioma;
//    private boolean firstSaoMiao = true;
//    private String police_code;
//    private Gson gson;
//    private SPUtils spUtils;
//
//    private Button bt_start_jiancha;   //开始消防器材检查
//    private Button to_end;    //结束消防器材检查
//    private RecyclerView recyclerView;
//    private RelativeLayout rl_title;
//
//    //上传pop
//    private TextView tv_qicai;
//    private TextView tv_is_guoqi;
//    private TextView tv_zichan;
//    private EditText et_err_pop_question;
//    private GridView gv_err_pop_photo;
//    private TextView btn_pop_save;
//    private TextView btn_pop_cancel;
//    private ImageButton ib_err_pop_exit;
//    private DeptCheckGVAdapter mAdapter;
//
//    //统计
//    private TextView tv_shidian;
//    private TextView tv_getnum_fire;
//
//    //请求接口
//    private Response_loadPoliceInfo loadPoliceInfo;
//    private String departmentId;
//    private String departmentName;
//    private String policeId;
//    private String mNowTime;
//    private String policeName;
//    private String cusNumber;
//    private String check_id;
//    //数量
//    private String pre_count;
//
//
//    private int RESULT_OK = -1;  //照相机默认为-1？
//    private static final int REQUEST_CODE = 555;
//    public static final int RESULT_CODE_QR_SCAN = 0xA1;
//    private CustomPopWindow fireUploadPop;
//    private List<Response_queryfire> response_queryfireList;
//    private String mziChanStr;
//
//    private int yiUpload = 0;
//    private FireListRecyclerViewAdapter fireListRecyclerViewAdapter;
//
//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        yiUpload = 0;
//        if(!Constance.isNeiMeng) {
//            showLoading();
//            initScan();   // 初始化广播
//        }else {
//            police_taioma = getIntent().getStringExtra("police_tiaoma");
//            getNetFromPoliceCode(police_taioma);
//        }
//    }
//
//    private void showLoading() {
//        loadingDialog = new LoadingDialog(this);
//        loadingDialog.setMessage("请扫描民警二维码").show();
//    }
//
//    @Override
//    public void onWidgetClick(View view) {
//        switch (view.getId()) {
//            case R.id.bt_start_jiancha:
//                if(Constance.isNeiMeng) {  //如果是演示版本则打开相机扫描
//                    //打开二维码扫描界面
//                    if(CommonUtil.isCameraCanUse()){
//                        Intent intent = new Intent(StartFireSearchActivity.this, CaptureActivity.class);
//                        startActivityForResult(intent, REQUEST_CODE);
//                    }else{
//                        Toast.makeText(this,"请打开此应用的摄像头权限！",Toast.LENGTH_SHORT).show();
//                    }
//                }else {
//                    //不是演示版本  则提示扫描消防器材条码
//                    loadingDialog.setMessage("请扫描消防器材").show();
//                }
//                break;
//            case R.id.to_end:   //调用结束接口
//                ToastUtils.showLong("结束按钮");
//                Request_fire_end request_fire_end = new Request_fire_end(cusNumber,check_id,TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
//                getP().setData(gson.toJson(request_fire_end),6);
//
//                break;
//
//        }
//    }
//
//    /**
//     * 扫描消防器材的回调
//     * @param requestCode
//     * @param resultCode
//     * @param data
//     */
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//
//        //通过scanResult 查询asset_code的值
//        if (resultCode == RESULT_CODE_QR_SCAN && requestCode == REQUEST_CODE) {
//            Bundle bundle = data.getExtras();
//            String scanResult = bundle.getString("qr_scan_result");
//
//            showPopFromScanResult(scanResult);
//
//
//        }else if(resultCode == RESULT_OK && (requestCode == TAKE_PHOTO_RESULT_CODE || requestCode == OPEN_PIC_RESULT_CODE)) {
//            String path = TakePhotoUtil.getInstance(StartFireSearchActivity.this).getCurPicPath(requestCode,data);
//            if(StringUtils.isEmpty(path)){
//                ToastUtils.showLong("照片路径错误");
//                return;
//            }
//            mAdapter.addOne(new Photo(path));
//        }
//    }
//
//    /**
//     * 摄像头扫描返回 和 利用手持机扫描返回
//     * @param scanResult
//     */
//    private void showPopFromScanResult(String scanResult) {
//        Response_queryfire scanBackFire = null;
//        for(int i = 0; i < response_queryfireList.size();i++) {
//            Response_queryfire response_queryfire = response_queryfireList.get(i);
//            if(scanResult.equals(response_queryfire.asset_code)) {
//                scanBackFire = response_queryfire;
//                break;
//            }
//        }
//        if(scanBackFire == null) {
//            ToastUtils.showLong("扫描的消防器材不在检查列表中");
//            return;
//        }
//        //显示PopupWindow
//        View contentView = LayoutInflater.from(StartFireSearchActivity.this).inflate(R.layout.pop_fire_toupdate,null);
//        handleContetView(contentView,scanResult,scanBackFire);
//        //显示大小
//        fireUploadPop = new CustomPopWindow.PopupWindowBuilder(this)
//                .setView(contentView)
//                .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
//                .create()
//                .showAsDropDown(rl_title, 0, 20);
//    }
//
//    private void handleContetView(View view, final String scanResult, Response_queryfire scanBackFire) {
//        tv_qicai = (TextView) view.findViewById(R.id.tv_qicai);
//        tv_is_guoqi = (TextView) view.findViewById(R.id.tv_is_guoqi);
//        tv_zichan = (TextView) view.findViewById(R.id.tv_zichan);
//        et_err_pop_question = (EditText) view.findViewById(R.id.et_err_pop_question);
//        gv_err_pop_photo = (GridView) view.findViewById(R.id.gv_err_pop_photo);
//        btn_pop_save = (TextView) view.findViewById(R.id.btn_pop_save);
//        btn_pop_cancel = (TextView) view.findViewById(R.id.btn_pop_cancel);
//        ib_err_pop_exit = (ImageButton) view.findViewById(R.id.ib_err_pop_exit);
//
//
//        //处理数据
//        tv_qicai.setText(scanResult);
//
//
//        String expired_date = scanBackFire.expired_date;  //失效日期
//        Long expiredMinis = TimeUtils.string2Millis(expired_date);  //失效日期毫秒时间戳
//        Long nowMunis = TimeUtils.getNowMills();   //当前毫秒事件戳
//        final String[] zichanStr = new String[]{"正常","异常"};
//        if(nowMunis > expiredMinis) {
//            tv_is_guoqi.setText("过期");
//            tv_zichan.setText("资产异常");
//            tv_zichan.setClickable(false);
//        }else {
//            tv_is_guoqi.setText("未过期");
//            tv_zichan.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    AlertDialog.Builder builder = new AlertDialog.Builder(StartFireSearchActivity.this);
//                    builder.setItems(zichanStr,new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//
//                            mziChanStr = zichanStr[which];
//                            tv_zichan.setText(mziChanStr);
//
//                            spUtils.put("mziChanStr",mziChanStr);
//                        }
//                    });
//                    builder.create().show();
//                }
//            });
//        }
//
//
//
//
//        mAdapter = new DeptCheckGVAdapter(this,new ArrayList<Photo>());
//        gv_err_pop_photo.setAdapter(mAdapter);
//        gv_err_pop_photo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                AlertDialog.Builder dialog = new AlertDialog.Builder(StartFireSearchActivity.this);
//                final CharSequence[] items = {"拍照","从相册中选择"};
//                dialog.setItems(items, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        if(which == 0){
//                            TakePhotoUtil.getInstance(StartFireSearchActivity.this).takePhoto();
//                        }else if(which == 1){
//                            TakePhotoUtil.getInstance(StartFireSearchActivity.this).openPic();
//                        }
//                    }
//                });
//                //显示提示框
//                dialog.show();
//            }
//        });
//
//
//        btn_pop_save.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String shuoming = et_err_pop_question.getText().toString().trim();
//                //调用服务器接口上传数据
//                String condition = tv_is_guoqi.getText() + ":" + shuoming;   //过期算资产状况
//                String time = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//
//                //点击保存， 则将集合中bean数据更新
//                for(int i = 0; i < response_queryfireList.size();i++) {
//                    //TODO 12月12日有bug， 上传成功后，应该remove掉
//                    Response_queryfire response_queryfire = response_queryfireList.get(i);
//                    if(scanResult.equals(response_queryfire.asset_code)) {
//                        response_queryfire.isSaveClick = true;
//                        response_queryfire.isGuoQi = tv_is_guoqi.getText().toString();
//                        response_queryfire.ziChan = tv_zichan.getText().toString();
//                        response_queryfire.shuoMing = shuoming;
//                        break;
//                    }
//                }
//                //刷新适配器
//                fireListRecyclerViewAdapter.notifyDataSetChanged();
//
//                //上传数据到服务器
//                JSONObject js = new JSONObject();
//                try {
//                    js.put("cus_number",cusNumber);
//                    js.put("check_id",check_id);
//                    js.put("police_id",policeId);
//                    js.put("asset_code",scanResult);
//                    js.put("condition",condition);
//                    js.put("time",time);
//                    String jsonObjectInfo = js.toString();
//                    ArrayList<File> pathList = new ArrayList<File>();
//                    for(int i= 0 ;i < mAdapter.getListData().size();i++) {
//                        pathList.add(new File(mAdapter.getListData().get(i).getPath()));
//                    }
//                    getP().uploadData(jsonObjectInfo, pathList);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//
//            }
//        });
//
//        btn_pop_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                fireUploadPop.dissmiss();
//            }
//        });
//        ib_err_pop_exit.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                fireUploadPop.dissmiss();
//            }
//        });
//
//    }
//
//    @Override
//    public void initData(Bundle savedInstanceState) {
//        gson = new Gson();
//        spUtils = SPUtils.getInstance("app_login");
//        recyclerView = (RecyclerView) findViewById(R.id.recyclerView_fire);
//        bt_start_jiancha = (Button) findViewById(R.id.bt_start_jiancha);
//        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
//        bt_start_jiancha.setOnClickListener(this);
//        to_end = (Button) findViewById(R.id.to_end);
//        to_end.setOnClickListener(this);
//        tv_shidian = (TextView) findViewById(R.id.tv_shidian);
//        tv_getnum_fire = (TextView) findViewById(R.id.tv_getnum_fire);
//
//    }
//
//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_start_fire_qingdian;
//    }
//
//    @Override
//    public PStartFireQingDian newP() {
//        return new PStartFireQingDian();
//    }
//
//
//    private void initScan() {
//        handleBroadcast = new HandleBroadcast(this, this);
//        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
//        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
//
//
//    }
//
//    @Override
//    public void updateText(String msg) {
//        //扫描头扫描的
//        soundpool.play(soundid, 1, 1, 0, 0, 1);
//        getNetFromPoliceCode(msg);
//    }
//
//    private void getNetFromPoliceCode(String msg) {
//        boolean status = msg.contains("P");
//        if(status && firstSaoMiao ) {
//            firstSaoMiao = !firstSaoMiao;
//            //是民警
//            police_code = msg.substring(1);
//            LogUtils.e(TAG,"获取民警信息" + police_code);
//            //查询民警信息
//            Request_loadPoliceInfo callroll_loadPoliceInfo = new Request_loadPoliceInfo();
//            callroll_loadPoliceInfo.setCusNumber(spUtils.getString("cusnumber"));
//            callroll_loadPoliceInfo.setPoliceCode(police_code);
//            //请求网络的入口
//            getP().setData(gson.toJson(callroll_loadPoliceInfo), 1);
//
//        }else {
//            //扫描的是消防器材
//            showPopFromScanResult(msg);   //扫描的msg 是和集合中的消防器材bean类的asset_code比较
//        }
//    }
//
//    public void setView(String data_json, int type) {
//        switch (type) {
//            case 1:
//                loadPoliceInfo = gson.fromJson(data_json, Response_loadPoliceInfo.class);
//                departmentId = loadPoliceInfo.getDepartment_id() + "";
//                departmentName = loadPoliceInfo.getDepartment_name();
//                policeId = loadPoliceInfo.getPolice_id();
//                policeName = loadPoliceInfo.getPolice_name();
//                mNowTime = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//                cusNumber = loadPoliceInfo.getCus_number() + "";
//                String policeName = loadPoliceInfo.getPolice_name();
//                //开始
//                Request_start_fire request_start_fire = new Request_start_fire(cusNumber,departmentId,departmentName,policeId,policeName,mNowTime);
//                getP().setData(gson.toJson(request_start_fire),2);
//                break;
//            case 2:   //开始接口返回
//                try {
//                    JSONObject jsonObject = new JSONObject(data_json);
//                    check_id = jsonObject.getString("check_id");
//                    pre_count = jsonObject.getString("pre_count");
//
//                    //加载列表
//                    Request_queryFireFighting query = new Request_queryFireFighting(cusNumber,departmentId,check_id);
//                    getP().setData(gson.toJson(query),3);
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//                break;
//            case 3:
//                LogUtils.e(data_json);
//                response_queryfireList = gson.fromJson(data_json,new TypeToken<List<Response_queryfire>>(){}.getType());
//                recyclerView.setLayoutManager(new LinearLayoutManager(StartFireSearchActivity.this));
//                fireListRecyclerViewAdapter = new FireListRecyclerViewAdapter(response_queryfireList);
//                recyclerView.setAdapter(fireListRecyclerViewAdapter);
//                if(response_queryfireList != null){
//                    tv_getnum_fire.setText(response_queryfireList.size() + "");
//                }
//                break;
//
//            case 5:
//                fireUploadPop.dissmiss();
//                ToastUtils.showLong("上传成功");
//                yiUpload++;
//                tv_shidian.setText(yiUpload + "");
//                break;
//
//            case 6:
//                ToastUtils.showLong("结束成功");
//                finish();
//                break;
//
//
//        }
//    }
//}
